package models

import (
	"github.com/jinzhu/gorm"
	"kop/modules/activity/entity"
)

func AutoMigrate(dB *gorm.DB) {

	dB.AutoMigrate(new(entity.ActivityPeerage))
	dB.AutoMigrate(new(entity.Pay))
	dB.AutoMigrate(new(entity.Quest))
	dB.AutoMigrate(new(entity.Ranking))
	dB.AutoMigrate(new(entity.TreasGuildRanking))
	dB.AutoMigrate(new(entity.GuildRanking))
	dB.AutoMigrate(new(entity.TreasMessage))
}

func GetActivityPeerage(dB *gorm.DB, roleID int32) entity.ActivityPeerage {

	var data = entity.ActivityPeerage{
		RoleID: roleID,
	}

	if err := dB.FirstOrCreate(&data).Error; err != nil {
		panic(err)
	}

	return data
}

func SaveActivityPeerage(dB *gorm.DB, data *entity.ActivityPeerage) {

	if err := dB.Save(data).Error; err != nil {
		panic(err)
	}
}

func GetPay(dB *gorm.DB, roleID int32) entity.Pay {

	var data = entity.Pay{
		RoleID: roleID,
	}

	if err := dB.FirstOrCreate(&data).Error; err != nil {
		panic(err)
	}

	return data
}

func SavePay(dB *gorm.DB, data *entity.Pay) {

	if err := dB.Save(data).Error; err != nil {
		panic(err)
	}
}

func GetQuest(dB *gorm.DB, roleID int32) entity.Quest {

	var data = entity.Quest{RoleID: roleID}
	if err := dB.FirstOrCreate(&data).Error; err != nil {
		panic(err)
	}

	return data
}

func SaveQuest(dB *gorm.DB, data *entity.Quest) {

	if err := dB.Save(data).Error; err != nil {
		panic(err)
	}
}
